<template>
  <!-- 新增部门的弹层 -->
  <el-dialog @close="btnCancel" :title="showTitle" :visible="showDialog">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form
      :model="formData"
      :rules="rules"
      ref="deptForm"
      label-width="120px"
    >
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="formData.name"
          style="width:80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="formData.code"
          style="width:80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manger">
        <el-select v-model="formData.manager" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="formData.introduce"
          style="width:80%"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
        />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button @click="btnOK" type="primary" size="small">确定</el-button>
        <el-button @click="btnCancel" size="small">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import {
  getDepartmentsApi,
  addDepartmentsApi,
  getDepartDetailApi,
  updateDepartmentsApi
} from "@/api/departments.js";
import { getEmployeeSimpleApi } from "@/api/employees.js";
import LogoVue from "@/layout/components/Sidebar/Logo.vue";
export default {
  // 需要传入一个props变量来控制 显示或者隐藏
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    // 当前操作的节点
    treeNode: {
      type: Object,
      default: null
    }
  },
  data() {
    var checkName = async (rule, value, callback) => {
      const res = await getDepartmentsApi();
      //console.log(res, this.treeNode, 44);
      let isRepeat = false;
      if (this.formData.id) {
        isRepeat = res.data.depts
          .filter(
            item => item.id !== this.formData.id && item.pid == this.treeNode.id
          )
          .some(item => item.name == value);
      } else {
        isRepeat = res.data.depts
          .filter(item => item.pid == this.treeNode.id)
          .some(item => item.name == value);
      }

      isRepeat
        ? callback(new Error(`同级部门下已经有${value}的部门了`))
        : callback();
    };
    var checkCode = async (rule, value, callback) => {
      //console.log(value, 6666);
      const res = await getDepartmentsApi();
      //console.log(res, 3344);
      const isRepeat = res.data.depts
        .filter(item => item.pid == this.treeNode.id)
        .some(item2 => item2.code == value);
      //console.log(isRepeat, 777);
      if (isRepeat) {
        callback(new Error(`同级部门下已经有${value}的编码了`));
      } else {
        callback();
      }
    };
    return {
      formData: {
        name: "", // 部门名称
        code: "", // 部门编码
        manager: "", // 部门管理者
        introduce: "" // 部门介绍
      },
      // 定义校验规则
      rules: {
        name: [
          { required: true, message: "部门名称不能为空", trigger: "blur" },
          {
            min: 1,
            max: 50,
            message: "部门名称要求1-50个字符",
            trigger: "blur"
          },
          {
            trigger: "blur",
            validator: checkName
          }
        ],
        code: [
          { required: true, message: "部门编码不能为空", trigger: "blur" },
          {
            min: 1,
            max: 50,
            message: "部门编码要求1-50个字符",
            trigger: "blur"
          },
          {
            trigger: "blur",
            validator: checkCode
          }
        ],
        manager: [
          { required: true, message: "部门负责人不能为空", trigger: "blur" }
        ],
        introduce: [
          { required: true, message: "部门介绍不能为空", trigger: "blur" },
          {
            trigger: "blur",
            min: 1,
            max: 300,
            message: "部门介绍要求1-50个字符"
          }
        ]
      },
      options: []
    };
  },
  created() {
    this.getEmployeeSimple();
  },
  computed: {
    showTitle() {
      return this.formData.id ? "编辑部门" : "新增子部门";
    }
  },
  methods: {
    async getEmployeeSimple() {
      const res = await getEmployeeSimpleApi();
      //console.log(res, 222);
      this.options = res.data;
    },
    btnOK() {
      this.$refs.deptForm.validate(async valide => {
        if (valide) {
          //有id是编辑
          if (this.formData.id) {
            await updateDepartmentsApi(this.formData);
          } else {
            await addDepartmentsApi({
              ...this.formData,
              pid: this.treeNode.id
            });
          }
          this.$emit("editDepts"); // 告诉父组件 编辑数据成功 重新拉取数据
          this.btnCancel();

          // const res = await addDepartmentsApi({
          //   ...this.formData,
          //   pid: this.treeNode.id
          // });
          // this.$emit("addDepts"); // 告诉父组件 新增数据成功 重新拉取数据
          // //console.log(res, 666);
          // this.btnCancel();
        }
      });
    },
    btnCancel() {
      // 重置数据  因为resetFields 只能重置 表单上的数据 非表单上的 比如 编辑中id 不能重置
      this.formData = {
        name: "",
        code: "",
        manager: "",
        introduce: ""
      };
      this.$refs.deptForm.resetFields(); // 重置校验字段
      this.$emit("update:showDialog", false);
    },

    async getDepartDetail(id) {
      const res = await getDepartDetailApi(id);
      console.log(res, 333333);
      //console.log(this.treeNode,4444);
      this.formData = res.data;
    }
  }
};
</script>
